#include <iostream>
#include <vector>
using namespace std;
#define maxn 101
#define maxm 10

int n,m;
char terrain[maxn][maxm];
int ban[maxn];
vector<int> can;
int dp[maxn][1<<maxm][1<<maxm];

int getBinarySum(int x){
	int ans=0;
	while(x!=0) {
		if((x&1)!=0) ans++;
		x>>=1;
	}
	return ans;
}

void init(){
	for(int i=0; i<(1<<m); i++){
		if((i&(i<<2)) == 0 && (i&(i<<1)) == 0){
			can.push_back(i);
			if((i & ban[0]) == 0)
				dp[0][i][0]=getBinarySum(i);
			if((i & ban[1]) == 0)
				dp[1][i][0]=getBinarySum(i);
		}
	}
	for(int p=0; p<can.size(); p++){
		int pstate = can[p];
		for(int c=0; c<can.size(); c++){
			int cstate = can[c];
			if((pstate & ban[0])==0 && (cstate & ban[1])==0 && (cstate & pstate)==0)
				dp[1][cstate][pstate] = getBinarySum(cstate)+getBinarySum(pstate);
		}
	}
}

int main(){
	cin>>n>>m;
	for(int i=0; i<n; i++){
		int cban = 0;
		for(int j=0; j<m; j++){
			cin>>terrain[i][j];
			if(terrain[i][j]=='H') cban += (1<<j);
		}
		ban[i] = cban;
	}
	init();
	for(int i=2; i<n; i++){
		for(int p=0; p<can.size(); p++){
			int pstate = can[p];
			if((pstate & ban[i-1]) == 0){
				for(int c=0; c<can.size(); c++){
					int cstate = can[c];
					if((cstate & ban[i])==0 && (cstate & pstate)==0){
						for(int pp=0; pp<can.size(); pp++){
							int ppstate = can[pp];
							if((ppstate & ban[i-2])==0 && (ppstate & pstate)==0 && (ppstate & cstate)==0)
								dp[i][cstate][pstate] = max(dp[i][cstate][pstate], dp[i-1][pstate][ppstate]+getBinarySum(cstate));
						}
					}
				}
			}
		}
	}

	int maxans = 0;
	for(int i=0; i<can.size(); i++)
		for(int j=0; j<can.size(); j++)
			if(dp[n-1][can[i]][can[j]] > maxans)
				maxans = dp[n-1][can[i]][can[j]];

	cout<<maxans<<endl;

	return 0;
}